package com.hunttown.mes.service;

import com.hunttown.mes.common.utils.FastJsonUtils;
import com.hunttown.mes.common.utils.GeneralUtils;
import com.hunttown.mes.common.utils.ScriptFilterUtils;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.dao.AnalysisManageDao;
import com.hunttown.mes.domain.AnalysisManage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * created by wangjunfu on 2021-02-25 10:27:38
 */
@Service
public class AnalysisManageService {

    private final static Logger logger = LoggerFactory.getLogger(AnalysisManageService.class);

    private final AnalysisManageDao analysisManageDao;
    private final AHelpService helpService;

    @Autowired
    public AnalysisManageService(AnalysisManageDao analysisManageDao, AHelpService helpService) {
        this.analysisManageDao = analysisManageDao;
        this.helpService = helpService;
    }

    /**
     * 新增
     *
     * @param analysisManage 实体对象
     * @return 最新插入id
     */
    public AnalysisManage insert(AnalysisManage analysisManage) {
        //api接口传进来是有值的
        if (analysisManage.getEntId() == null)
            analysisManage.setEntId(helpService.getEnterpriseId());
        if (analysisManage.getCreateTime() == null)
            analysisManage.setCreateTime(new Date());
        if (analysisManage.getStopFlag() == null)
            analysisManage.setStopFlag(0);
        if (analysisManage.getDeleteFlag() == null)
            analysisManage.setDeleteFlag(0);

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(analysisManage), AnalysisManage.class))) {
            return analysisManage;
        }
        return analysisManageDao.insert(analysisManage);
    }

    /**
     * 根据ID修改数据信息
     *
     * @param analysisManage 实体对象
     * @return True or False
     */
    public Boolean updateInfoById(AnalysisManage analysisManage) {
        if (analysisManage.getEntId() == null) {
            analysisManage.setEntId(helpService.getEnterpriseId());
        }

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(analysisManage), AnalysisManage.class))) {
            return false;
        }
        return analysisManageDao.updateInfoById(analysisManage);
    }

    /**
     * 根据Query修改数据信息
     *
     * @param query 需要修改的字段 KV 格式
     * @return True or False
     */
    public Boolean updateInfoByQuery(Query query) {
        if (!query.containsKey("w_entId")) {
            query.put("w_entId", helpService.getEnterpriseId());
        }

        if (ScriptFilterUtils.xssFilterForQuery(query)) {
            return false;
        }
        return analysisManageDao.updateInfoByQuery(query);
    }

    /**
     * 根据ID获取记录信息
     *
     * @param id 主键id
     * @return 实体对象
     */
    public AnalysisManage selectById(Integer id) {
        return analysisManageDao.selectById(id, helpService.getEnterpriseId());
    }

    //仅供登录使用，不考虑企业ID
    public AnalysisManage selectByMobileForLogin(String mobile) {
        return analysisManageDao.selectByMobileForLogin(mobile);
    }

    /**
     * 根据query取出一条记录，但是可能有多条记录，取最新的一条。
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象
     */
    public AnalysisManage selectObjByQuery(Query query) {
        if (query == null || query.size() == 0) {
            return null;
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        List<AnalysisManage> list = selectByQuery(query);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    /**
     * 根据条件获取List
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象列表
     */
    public List<AnalysisManage> selectByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return analysisManageDao.selectByQuery(query);
    }

    /**
     * 根据条件获取记录总数
     *
     * @param query 查询条件 KV 格式
     * @return 总记录数
     */
    public Long countByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return analysisManageDao.countByQuery(query);
    }

    /**
     * 根据ID删除记录信息
     *
     * @param id 主键id
     * @return True or False
     */
    public Boolean deleteById(Integer id) {
        return analysisManageDao.deleteById(id, helpService.getEnterpriseId());
    }

    /**
     * 模糊搜索管理员
     *
     * @param query 条件
     * @return list
     */
    public List<Map<String, String>> selectSimilarManage(final Query query) {
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }
        if (!query.containsKey("stopFlag")) {
            query.put("stopFlag", 0);
        }
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        return analysisManageDao.selectSimilarManage(query);
    }

    //获取用户权限菜单代码集合
    public Map<String, String> selectManagerRoleMenu(Query query) {
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }
        return analysisManageDao.selectManagerRoleMenu(query);
    }

    //根据用户user_id获取用户微信open_id
    public String getDeviceByMobile(String mobile, Integer entId) {
        Query query = Query.noPage();
        query.put("entId", entId);
        query.put("mobile", mobile);
        query.put("stopFlag", 0);
        AnalysisManage obj = selectObjByQuery(query);
        return obj == null ? "" : obj.getDeviceKey();
    }
}